package jack.handlerdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import jack.handler.Handler;
import jack.handler.Looper;
import jack.handler.Message;

public class MainActivity extends AppCompatActivity {

    private TextView mViewById;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewById = (TextView) findViewById(R.id.tv);

        //记录问题：增加了该方法后，view的内容为白屏
//        sendMsg();

    }

    private void sendMsg() {
        Looper.prepare();

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                System.out.println("Thread： " + Thread.currentThread().getName() + " received: " + msg);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mViewById.setText("12345");
                        System.out.println("Thread： " + Thread.currentThread().getName() + " received3: " );
                    }
                });
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message message = new Message();
                message.obj = "hello";
                message.what = 1;
                System.out.println("Thread： " + Thread.currentThread().getName() + " send: " + message);
                handler.sendMessage(message);

            }
        }).start();
        Looper.loop();
    }


}
